import Vue from 'vue';
import 'assets/style/global';
import axios from './utils/axios';

import ZeesuuService from '@zeesuu/service';
import APIs from 'config/API';
import Toast from './components/Toast';
import createVideoBox from './components/Player';
import qs from 'qs';
const query = qs.parse(window.location.search.replace(/^\?/, ''));
Vue.prototype.$Toast = Toast;

import './assets/svg';

import AppJump from './directive/AppJump';
Vue.use(AppJump);

// pv上报
import MutongPv from 'mutong-pv';
// 当做插件加载
Vue.use(MutongPv, {
  globalOption: {
    pt: '',
  },
});

// 在PC/H5下要用这个来做环境
import JHWE from 'jimu-hybrid-web-environment';
Vue.use(JHWE, {
  callback: {
    previewPhoto(data) {
      console.log('preview', data);
    },
    selectPhoto(data) {
      console.log('conf', data);
    },
    selectVideo() {},
    openVideo({ vid, event }) {
      createVideoBox(event.target.parentNode, {
        vid,
      });
    },
    toast(data) {
      Toast(data.msg);
    },
    appInfo() {
      return Promise.resolve({
        data: { dark: 0 },
        code: 0,
      });
    },
  },
});

import appService from 'mutong-app-service';
Vue.use(appService);

const lowerUA = window.navigator.userAgent.toLowerCase();
// Vue.use(ZeesuuService, {
//   $http: axios,
//   apis: APIs,
//   debug: process.env.NODE_ENV === 'development',
// });
// Service
// hybrid 里边也包含一个 service，会被重复覆盖，在APP内的使用APP的请求方式吧
Vue.use(
  { ...ZeesuuService },
  {
    apis: APIs,
    $http:
      lowerUA.indexOf('jimu') > -1
        ? Vue.prototype.$appService && Vue.prototype.$appService.network
        : axios,
    isMini: lowerUA.indexOf('jimu') > -1,
    debug: process.env.NODE_ENV === 'development',
  },
);

// add Hybrid
import JimuHybrid from 'jimu-hybrid';
Vue.use(JimuHybrid);

Vue.filter('imgSize', (url, w, h) => {
  if (!url) return;
  let a = '?x-oss-process=image/resize,m_fill';
  if (w) {
    a = a + ',w_' + w;
  }
  if (h) {
    a = a + ',h_' + h;
  }
  return url + a;
});

Vue.prototype.$UA = {
  isJM: lowerUA.indexOf('jimu') > -1,
  isAndroid: lowerUA.match(/android/i),
  isIOS: lowerUA.match(/(iphone|ipod|ios)/i),
  isMobile: lowerUA.match(/(iphone|ipod|ios|android)/i),
};

Vue.prototype.$service
  .UserWechatJsapiTicket({ url: window.location.href })
  .then((res) => {
    wx.config({
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: res.appId, // 必填，公众号的唯一标识
      timestamp: res.timestamp, // 必填，生成签名的时间戳
      nonceStr: res.nonceStr, // 必填，生成签名的随机串
      signature: res.signature, // 必填，签名，见附录1
      jsApiList: [
        'onMenuShareWeibo',
        'onMenuShareQZone',
        'updateTimelineShareData',
        'updateAppMessageShareData',
      ],
      openTagList: ['wx-open-launch-app'],
    });
    Vue.prototype.$setShareInfo = (data) => {
      const { image, subTitle, title, url } = data;
      wx.updateAppMessageShareData({
        title: title, // 分享标题
        desc: subTitle, // 分享描述
        link: url || window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: image, // 分享图标
        success: function() {
          // 设置成功
        },
      });
      wx.updateTimelineShareData({
        title: title, // 分享标题
        desc: subTitle, // 分享描述
        link: url || window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: image, // 分享图标
        success: function() {
          // 设置成功
        },
      });
    };
  })
  .catch((err) => {
    console.log(err);
  });

Vue.config.ignoredElements = ['wx-open-launch-app'];

// 在APP内打开这些页面唤起原生页
if (Vue.prototype.$UA.isJM) {
  const href = window.location.href;
  let appScheme = '';
  if (href.includes('works.html') || href.includes('article.html')) {
    appScheme = `mtjimu://works?id=${query.id}`;
  }
  if (href.includes('topic.html')) {
    appScheme = `mtjimu://topic?id=${query.id}`;
  }
  if (href.includes('opinion.html')) {
    appScheme = `mtjimu://opinion?id=${query.id}`;
  }
  if (href.includes('user.html')) {
    appScheme = `mtjimu://homepage?uid=${query.id}`;
  }
  if (href.includes('demanddetail.html')) {
    appScheme = `mtjimu://demand?id=${query.id}`;
  }
  if (href.includes('jobdetail.html')) {
    appScheme = `mtjimu://job?id=${query.id}`;
  }
  // 有的话去原生，没有的话去h5
  if (appScheme) {
    Vue.prototype.$appService.openLink({
      link: appScheme,
    });
    Vue.prototype.$appService.exit();
    // window.location.href = appScheme;
  }
}

export default Vue;
